<?php
namespace app\admin\controller;

use think\Db;
use think\Session;
use think\Controller;

class Login extends Base
{
	public function _initialize(){
	}
	public function index()
	{   
		$isLogin=$this->isLogin();//halt($isLogin);
		if($isLogin){
			return $this->redirect('admin/index/index');
		}else{
			//如果后台用户已经登录了，那么我们需要跳转到后台页面
			return $this->fetch();
		}
	}

    //管理员登录
    public function check(){
		if(request()->isAjax())
        {
            $data=input('post.');//halt($data);
			$gname=$data['gname'];
			$pwd=md5($data['pwd']);
			$rs=db('guanli')->where(['gname'=>$gname])->find();//halt($rs);
            if($data['code']==''){
                return array('code'=>-1,'msg'=>"请输入验证码");
            }
			if(!captcha_check($data['code'])){
				return array('code'=>-1,'msg'=>"验证码错误");
		    }
			if ($rs)
			{
				$cha=db('guanli')->where(['gname'=>$gname,'pwd'=>$pwd])->find();
				if($cha)
				{
					Session::set('gname',$gname);
					Session::set('admin_uid',$cha['id']);
                    return array('code'=>1,'msg'=>"登录成功");
                }
				else
				{
					return array('code'=>0,'msg'=>"登录失败,密码错误！");
				}
            }
			else
				{
					return array('code'=>0,'msg'=>"登录失败,账号错误！");
			    }
		     }
		else
		{
            return view('admin/login/index');
        }
	}

    public function welcome()
    {
        return view('welcome');
    }
	

    //退出登录代码
    public function tuichu()
    {
        Session(null);
        $this->redirect('admin/login/index');
    }
}
